package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.ReviewsAdd;

public class GenericReviewsAddDAO extends AutoKeys implements
		org.jetnuke.dao.ReviewsAddDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericReviewsAddDAO.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.ReviewsAddModel#addNew(org.jetnuke.entities.ReviewsAdd)
	 */
	public int addNew(ReviewsAdd rv) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(ReviewsAdd) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("ReviewsAddModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("ReviewsAddModel.lastGeneratedReviewAddID"));
		initNewReviewAdd(rv, p);

		int id = this.executeAutoKeysQuery(p);

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(ReviewsAdd) - end"); //$NON-NLS-1$
		}
		return id;
	}

	/**
	 * @param rv
	 * @param p
	 */
	protected void initNewReviewAdd(ReviewsAdd rv, PreparedStatement p)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("initNewReviewAdd(ReviewsAdd, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setTimestamp(1, new Timestamp(rv.getDATE().getTime()));
		p.setString(2, rv.getEMAIL());
		p.setString(3, rv.getREVIEWER());
		p.setString(4, rv.getRLANGUAGE());
		p.setInt(5, rv.getSCORE());
		p.setString(6, rv.getTEXT());
		p.setString(7, rv.getTITLE());
		p.setString(8, rv.getURL());
		p.setString(9, rv.getURL_TITLE());

		if (logger.isDebugEnabled()) {
			logger
					.debug("initNewReviewAdd(ReviewsAdd, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.ReviewsAddModel#countAll()
	 */
	public int countAll() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("countAll() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("ReviewsAddModel.countAll"));

		ResultSet rs = p.executeQuery();
		rs.next();

		int r = rs.getInt(1);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("countAll() - end"); //$NON-NLS-1$
		}
		return r;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.ReviewsAddModel#delete(int)
	 */
	public void delete(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("delete(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("ReviewsAddModel.delete"));
		p.setInt(1, id);
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("delete(int) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.ReviewsAddModel#selectAllOrderID()
	 */
	public List selectAllOrderID() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAllOrderID() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("ReviewsAddModel.selectAllOrderID"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAllOrderID() - end"); //$NON-NLS-1$
		}
		return list;
	}

	/**
	 * @param rs
	 * @return
	 */
	private List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			ReviewsAdd u = new ReviewsAdd();
			fillReviewsAddFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/**
	 * @param u
	 * @param rs
	 */
	private void fillReviewsAddFromResultSet(ReviewsAdd u, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillReviewsAddFromResultSet(ReviewsAdd, ResultSet) - start"); //$NON-NLS-1$
		}

		u.setDATE(rs.getTimestamp("DATE"));
		u.setEMAIL(rs.getString("EMAIL"));
		u.setID(rs.getInt("ID"));
		u.setREVIEWER(rs.getString("REVIEWER"));
		u.setRLANGUAGE(rs.getString("RLANGUAGE"));
		u.setSCORE(rs.getInt("SCORE"));
		u.setTEXT(rs.getString("TEXT"));
		u.setTITLE(rs.getString("TITLE"));
		u.setURL(rs.getString("URL"));
		u.setURL_TITLE(rs.getString("URL_TITLE"));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillReviewsAddFromResultSet(ReviewsAdd, ResultSet) - end"); //$NON-NLS-1$
		}
	}

}
